package com.smart.car.points.service.impl;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.smart.car.common.res.bean.ResponseResult;
import com.smart.car.common.res.exception.BusinessException;
import com.smart.car.points.entity.PointsInfo;
import com.smart.car.points.entity.PointsInfoExample;
import com.smart.car.points.mapper.PointsInfoMapper;
import com.smart.car.points.service.PointsInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class PointsInfoServiceImpl implements PointsInfoService {

    @Autowired
	private PointsInfoMapper infoMapper;

    @Override
    public List<PointsInfo> list() {
        PointsInfoExample example = new PointsInfoExample();
        List<PointsInfo> infoList = infoMapper.selectByExample(example);
        return infoList;
    }


    @Override
    public int addMemberCard(String json) throws BusinessException {
        PointsInfo card = JSONObject.parseObject(json,PointsInfo.class);
        log.info("新增会员积分信息： " +json);
        return infoMapper.insertSelective(card);
    }

    @Override
    public int updateMemberCard(String json) {
        PointsInfo card = JSONObject.parseObject(json,PointsInfo.class);
        return infoMapper.updateByPrimaryKeySelective(card);
    }

    @Override
    public PointsInfo getMemberCard(String memberId) {
        return infoMapper.selectByPrimaryKey(memberId);
    }

    @Override
    public ResponseResult getPointsById(String pointsId) {
        PointsInfoExample example = new PointsInfoExample();
        PointsInfoExample.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotEmpty(pointsId)){
            criteria.andMemberIdEqualTo(pointsId);
        }
        List<PointsInfo> pointsInfos = infoMapper.selectByExample(example);
        return ResponseResult.ok(pointsInfos);
    }

    @Override
    public ResponseResult getPointsByPhone(String pointsId) {
        PointsInfoExample example = new PointsInfoExample();
        PointsInfoExample.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotEmpty(pointsId)){
            criteria.andMemberIdEqualTo(pointsId);
        }
        List<PointsInfo> pointsInfos = infoMapper.selectByExample(example);
        return ResponseResult.ok(pointsInfos);
    }

}
